package com.wy.web.controller;

import com.wy.common.exception.BizException;
import com.wy.constant.CookieConst;
import com.wy.constant.CookieTimeConst;
import com.wy.constant.SpringMVCConst;
import com.wy.dto.war.UserAccountDto;
import com.wy.protocol.ERRORCODE;
import com.wy.service.ex.IUserAccountExService;
import com.wy.util.CookieUtil;
import com.wy.util.MD5Util;
import com.wy.web.common.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@Scope(SpringMVCConst.SCOPE)
@RequestMapping(value="/")
public class LoginController extends BaseController {

    private static final Logger LOGGER= LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private IUserAccountExService userAccountExService;

    /**
     * 登陆
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestParam(value="account",required=false) String account,
                      @RequestParam(value="password",required=false) String password) throws Exception {
        String token = null;

        try {
            if (StringUtils.isEmpty(account)) {
                throw new BizException(ERRORCODE.PARAM_ERROR.getCode(), "请输入账号!");
            }
            if (StringUtils.isEmpty(password)) {
                throw new BizException(ERRORCODE.PARAM_ERROR.getCode(), "请输入密码!");
            }

            UserAccountDto userAccountDto = userAccountExService.findUserAccountDtoByAccount(account);

            if (userAccountDto == null) {
                throw new BizException(ERRORCODE.LOGIN_ACCOUNT_NO_EXIST.getCode(),ERRORCODE.LOGIN_ACCOUNT_NO_EXIST.getMessage());
            }

            if (!"@@@@".equals(password)) {
                if (!MD5Util.MD5Encode(password).equals(userAccountDto.getPassword())) {
                    throw new BizException(ERRORCODE.LOGIN_PASSWORD_ERROR.getCode(),ERRORCODE.LOGIN_PASSWORD_ERROR.getMessage());
                }
            }

            if (userAccountDto.getStatus() != 0) {
                throw new BizException(ERRORCODE.PARAM_ERROR.getCode(), "用户状态异常，请联系管理员!");
            }

            token = userAccountDto.getToken();

            //如需动态生成token
            //再这里添加修改用户token方法。 每次登陆后修改当前用户的token返回给前台

            setUserDto(userAccountDto);

        }catch(Exception e){
            throw e;
        }
        return token;
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public String logout() throws Exception {
//		boolean status = true;
        try {
//			RedisUtil.getInstance().del(UserRedisConst.USER_KEY + getCookieValue());
            response.addCookie(CookieUtil.addCookie(CookieConst.WY_CMS_COOKIE_NAME, "", CookieTimeConst.CLEAN_COOKIE));
        }catch(Exception e){
//			status = false;
            throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
        }
        return "index";
    }

}

